home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr13 / aurora2c.zip / SUMBLOCK.AML < prev    next >
Text File  |  1995-04-07  |  3KB  |  113 lines

  1.  
  2. // ───────────────────────────────────────────────────────────────────
  3. // The Aurora Editor v2.0
  4. // Copyright 1993-1995 nuText Systems. All Rights Reserved Worldwide.
  5. //
  6. // Add up numbers in a marked block
  7. //
  8. // This macro adds up numbers in a marked block in the current window
  9. // and displays the total. It will handle numbers from -2 billion to
  10. // +2 billion with a maximum of 8 decimal places.
  11. // ───────────────────────────────────────────────────────────────────
  12.  
  13.   // compile time macros and function definitions
  14.   include  bootpath "define.aml"
  15.  
  16.   var width
  17.   var frac_total
  18.   var int_total
  19.   var count
  20.  
  21.   // do only if a mark exists in current window
  22.   if getcurrbuf <> getmarkbuf then
  23.     msgbox "Block not marked in current window"
  24.     return
  25.   end
  26.  
  27.   // close mark and save cursor position
  28.   stopmark
  29.   pushcursor
  30.  
  31.   loop
  32.  
  33.     // find numbers in the mark using regular expressions
  34.     width = find "-?{[0-9]*\\.[0-9]#}|{[0-9]#}"
  35.                  (if? width "xb*" "xbg*")
  36.     if width then
  37.  
  38.       // count numbers found
  39.       count = count + 1
  40.  
  41.       // get number and position of decimal character
  42.       number = gettext (getcol) width
  43.       p = pos '.' number
  44.  
  45.       // handle fractional portion of number
  46.       if p then
  47.         if p < (sizeof number) then
  48.  
  49.           // total up fraction portion in frac_total
  50.           f = pad  number [p + 1 : TO_END]  8 'l' '0'
  51.           frac_total = if (pos '-' number) then
  52.                          frac_total - f
  53.                        else
  54.                          frac_total + f
  55.                        end
  56.  
  57.           // negative fractional sum
  58.           if frac_total < 0 then
  59.             int_total = int_total - 1
  60.             frac_total = frac_total + 100000000
  61.  
  62.           // fractional overflow
  63.           elseif frac_total > 99999999 then
  64.             overflow = (sizeof frac_total) - 8
  65.             int_total = int_total + frac_total [1 : overflow]
  66.             frac_total = frac_total [overflow + 1 : TO_END]
  67.           end
  68.         end
  69.         number = number [1 : p - 1]
  70.       end
  71.  
  72.       // add up integer portion
  73.       int_total = int_total + number
  74.  
  75.       // setup for next search
  76.       right width
  77.     else
  78.       break
  79.     end
  80.   end
  81.  
  82.   // restore cursor position and update display
  83.   popcursor
  84.   display
  85.  
  86.   if frac_total then
  87.  
  88.     // compensate for negative integer sum
  89.     // and positive fraction sum
  90.     if int_total < 0 then
  91.       int_total = int_total + 1
  92.       frac_total = 100000000 - frac_total
  93.     end
  94.  
  95.     // right justify fraction and pad with zeros
  96.     frac_total = pad frac_total 8 '' '0'
  97.  
  98.     // remove trailing zeros and combine integer and
  99.     // fraction portion
  100.     int_total = int_total + '.' +
  101.                   frac_total [1 : posnot '0' frac_total 'r']
  102.   end
  103.  
  104.   if not int_total then
  105.     int_total = 0
  106.   end
  107.  
  108.   // display the sum and prompt the user to enter
  109.   if (okbox  "Sum of " + count + " numbers is: " + int_total  + ".  Enter into text?" "Block Sum") == "Ok" then
  110.     write int_total
  111.   end
  112.  
  113.